<?php
class Tour_BlockController extends Clu_Controller_Action_Mini
{
	
	public function init()
	{
		//require_once (APP_PATH.'/modules/article/Model/Article.php');
		//require_once (APP_PATH.'/modules/tour/Model/Tour.php');
		//require_once (APP_PATH.'/modules/tour/Model/TLeg.php');
		$this->mArticle = new article_Model_Service();
		$this->m = new Model_Tour();
		$this->tl = new Model_TLeg();
		parent::init();
	}
	
	public function getListTour($data)
	{
		if(isset($data['item_per_page']) == null)
			$data['item_per_page'] = 10 ;
		if(isset($data['page']) == null)
			$data['page'] = 1 ;
	   	$select = $this->m->search($data);
	   	$paginator = $this->generatePaginator($select, $data['page'] , $data['item_per_page'], 7, 'Jumping');
	   	$sess = new Zend_Session_Namespace("paginatorTour");
	   	$sess->tour_list = array("tour_params" => $data);
        $data = $sess->tour_list;
				
      	//cache in $_SESSION
        if(isset($data['page']))
        {
   			$select = $this->m->search($data['tour_params']);
       		$paginator = $this->generatePaginator($select, $this->_getParam('page'), $data['item_per_page'],7, 'Jumping');
        }
        return $paginator;
	}
	
	public function generatePaginator($select, $pageNumber, $itemsPerPage, $pageRange, $style)
    {
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		$paginator->setItemCountPerPage($itemsPerPage);
		$paginator->setCurrentPageNumber($pageNumber);
		$paginator->setPageRange($pageRange);
		Zend_Paginator::setDefaultScrollingStyle($style);
		return $paginator;
    }
    
    public function relatedUserAction()
    {
    	$id = $this->miniRequest['id'];
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->view->id = $id;
		
		$data = array('poster_id' => $id,'item_per_page' => 5 );
		$paginatorTour = $this->getListTour($data);
       	$this->view->paginator = $paginatorTour;
		$this->view->listTours = $paginatorTour->getCurrentItems();
		$this->renderToHolder('block/related-to-user.phtml', false, null, $regionId, $boxWeight);
    
    }
    
    public function relatedTourAction()
    {
    	$id = $this->miniRequest['poster_id'];
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->view->id = $id;
		
		$data = array('poster_id' => $id);
		$paginatorTour = $this->getListTour($data);
       	$this->view->paginator = $paginatorTour;
		$this->view->listTours = $paginatorTour->getCurrentItems();
		$this->renderToHolder('block/related-to-place.phtml', false, null, $regionId, $boxWeight);
    
    }
    
	public function featuredListAction()
    {
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		
		//departure_date and finish_date in about 90 days
		//$outbound = array('departure_date' => date("d/m/Y",time()), 'return_date' => date("d/m/Y",time() + 7776000),'category' => 'outbound','status' => 'featured','item_per_page' => 3);
		//$inbound   = array('departure_date' => date("d/m/Y",time()), 'return_date' => date("d/m/Y",time() + 7776000),'category' => 'inbound','status' => 'featured','item_per_page' => 3);
		$outbound = array ('status' => 'published', 'category' => 'outbound');
		$inbound = array ('status' => 'published', 'category' => 'inbound', 'item_per_page' => 6);
		//inbound tour
		$paginatorTour = $this->getListTour($inbound);
       	$this->view->paginator = $paginatorTour;
		$this->view->inboundTourList = $paginatorTour->getCurrentItems();
		
		//outbound tour
		$paginatorTour = $this->getListTour($outbound);
       	$this->view->paginator = $paginatorTour;
		$this->view->outboundTourList = $paginatorTour->getCurrentItems();
//		die_a($this->view->inboundTourList);
    	//get articles
		$articleList = array();
		$mArticle = new article_Model_Service();
		if (count ($this->view->inboundTourList ) > 0 )
		{
			foreach ($this->view->inboundTourList as $t)
			{
				$articleList[$t['id']] = $mArticle->getArticleData($t['article_id']);
			}
//			die_a($articleList);
			$this->view->inboundArticleList = $articleList;
		}
			
    	if ( $this->miniRequest['page'] == 'home')
    	{
    		$this->renderToHolder('block/featured-tour-list-home.phtml', false, null, $regionId, $boxWeight);
    	}
    }
    
	public function topTourListAction()
    {
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		
		$inbound   = array('category' => 'inbound','status' => 'published','item_per_page' => 4);
		//inbound tour
		$paginatorTour = $this->getListTour($inbound);
       	$this->view->paginator = $paginatorTour;
		$this->view->inboundTourList = $paginatorTour->getCurrentItems();
		$this->renderToHolder('block/5/top-tour-list.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function simpleSearchAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
    	$form = new Form_TourSearch();
        $form->cluBuild(array('action' => 'search', 'view' => $this->view, 'cluSearchMode' => 'simpleSearch'));
        $this->view->form = $form;
		$this->renderToHolder('block/simple-search.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function hotTourListAction()
    {
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		
		$inbound   = array('status' => 'published','item_per_page' => 4);
		//inbound tour
		$paginatorTour = $this->getListTour($inbound);
       	$this->view->paginator = $paginatorTour;
		$this->view->tourList = $paginatorTour->getCurrentItems();
		
		//get articles
		$articleList = array();
		$mArticle = new article_Model_Service();
		if (count ($this->view->tourList ) > 0 )
		{
			foreach ($this->view->tourList as $t)
			{
				$articleList[$t['id']] = $mArticle->getArticleData($t['article_id']);
			}
			$this->view->tourArticleList = $articleList;
		}
		$this->renderToHolder('block/5/hot-tour-list.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function listInBoundAction()
    {
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		
		$inbound = array ('status' => 'published', 'category' => 'inbound', 'item_per_page' => 6);
		//inbound tour
		$paginatorTour = $this->getListTour($inbound);
		foreach($paginatorTour->getCurrentItems() as $v)
		{
			if($v['type'] == 'inbound')
				$results[] = $v; 
		}
       	$this->view->paginator = $paginatorTour;
		//$this->view->inboundTourList = $paginatorTour->getCurrentItems();
		$this->view->inboundTourList = $results;
    	//get articles
		$articleList = array();
		$mArticle = new article_Model_Service();
		if (count ($this->view->inboundTourList ) > 0 )
		{
			foreach ($this->view->inboundTourList as $t)
			{
				$articleList[$t['id']] = $mArticle->getArticleData($t['article_id']);
			}
			$this->view->inboundArticleList = $articleList;
		}
   		$this->renderToHolder('block/inbound-tour-list.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function listOutBoundAction()
    {
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$outbound = array ('status' => 'published', 'category' => 'outbound', 'item_per_page' => 6);
		
		//outbound tour
		$paginatorTour = $this->getListTour($outbound);
		foreach($paginatorTour->getCurrentItems() as $v)
		{
			if($v['type'] == 'outbound')
				$results[] = $v; 
		}
       	$this->view->paginator = $paginatorTour;
		//$this->view->outboundTourList = $paginatorTour->getCurrentItems();
		$this->view->outboundTourList = $results;
    	//get articles
		$articleList = array();
		$mArticle = new article_Model_Service();
		if (count ($this->view->outboundTourList ) > 0 )
		{
			foreach ($this->view->outboundTourList as $t)
			{
				$articleList[$t['id']] = $mArticle->getArticleData($t['article_id']);
			}
			
			$this->view->outboundArticleList = $articleList;
		}
   		$this->renderToHolder('block/outbound-tour-list.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function viewTourAction() 
	{
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$id = $this->miniRequest['id'];
		$tour = $this->m->getTourData($id, true, true);
        $a = $this->mArticle->getArticleData($tour['article_id']);
        $tour['article'] = $a;
        $aTleg = $this->tl->getLegData($id, true);
        $tlegData = $this->tl->getLegsByTourId($id);
        if(isset($tlegData))
        {
	        foreach($tlegData as $aid)
	        	$lidArray[] = $aid['leg']['article_id'];
	    	$aTleg = $this->tl->getLegData($lidArray, true);
	   		$this->view->aTlegData = $aTleg;
        }        	
        $this->view->tour = $tour;
        
		$sess = new Zend_Session_Namespace(CLU_NS);
        $auth = Zend_Auth::getInstance();
        $this->view->edit_allowed = false;
        if (strpos ($sess->userPermissions, 'update tour'))//tour admin
        	$this->view->edit_allowed = true;
		elseif ($auth->hasIdentity())
		{
            $uIdentity = $auth->getIdentity();
    		if (isset($uIdentity->id) == $tour['poster_id'] && strpos ($sess->userPermissions, 'update own tour'))
    		{
    			$this->view->edit_allowed = true;
    		}
        }

        $this->renderToHolder('block/view-tour.phtml', false, null, $regionId, $boxWeight);
	}
	
	public function saleOffAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->renderToHolder('block/saleOff.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function advisoryAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->renderToHolder('block/advisory.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function listCompanyAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->renderToHolder('block/list-company.phtml', false, null, $regionId, $boxWeight);
    }
}	
?>